package arithmetic.LeetCode;

/**
 * @author jiangfeng on 2022/11/25
 */
public class JiduTest {

    static class Node {
        Node next;
        int val;

        public Node() {
        }

        public Node(Node next, int val) {
            this.next = next;
            this.val = val;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }
    }

    /**
     * 删除重复链表
     */
    public static void main(String[] args) {
        Node node = genLinkList(new int[] {1, 1, 2, 2, 3});
        Node node1 = genLinkList(new int[] {1, 1,1, 2, 2, 3,3});
        printLink(node);
        removeRepeatNode(node);
        printLink(node);
    }

    public static Node removeRepeatNode(Node head) {
        Node cur = head;
        Node noRepeat = head;
        while (cur != null && cur.next != null) {
            if (cur.next.val == noRepeat.val) {
                cur = cur.next;
                continue;
            }
            noRepeat.next = cur.next;
            noRepeat = noRepeat.next;
            cur = cur.next;
        }
        return head;
    }

    public static void printLink(Node head) {
        while (head != null) {
            System.out.print(head.val + "->");
            head = head.next;
        }
        System.out.println();
    }

    /**
     * shen'c
     */
    public static Node genLinkList(int[] arr) {
        Node head = new Node(null, arr[0]);
        Node pre = null;
        Node cur = head;
        for (int i = 1; i < arr.length; i++) {
            pre = new Node(null, arr[i]);
            cur.next = pre;
            cur = pre;
        }
        return head;
    }
}
